home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 11 / Mac Magazin and MacEasy Magazine CD - Issue 11.iso / Sharewarebibliothek / Entwickler / appe Windows 2.0 / AppeWin for SC7 / filter.c < prev    next >
Text File  |  1995-06-11  |  13KB  |  339 lines

  1. // File "filter.c" -
  2.  
  3. #include <TextServices.h>
  4.  
  5. #include "main.h"
  6. #include "filter.h"
  7. #include "floaters.h"
  8.  
  9. // ***********************************************************************************
  10. // Global Declarations 
  11.  
  12. extern GlobalsRec glob;
  13.  
  14. // ***********************************************************************************
  15. // ***********************************************************************************
  16.  
  17. /*
  18.     The Not-So-Simple FAT jGNEFilter 
  19.         Original Code by Matt Slot (fprefect@umich.edu), 6/2/95 
  20.             with help and criticism (lots!) from Ed Wynne (arwyn@umich.edu).
  21.     
  22.     Quick Intro
  23.         Since jGNEFilters are nasty things in 68k, and pretty much impossible 
  24.         in PPC, writing simple and cross-compiling handler code is also. To 
  25.         facilitate use of this, I have written some interface routines to hide
  26.         some of the complexity from the application programmer.
  27.         
  28.     /    Ptr InstallEventFilter(FilterHelperProcPtr helperProc, Ptr helperData);
  29.     |        Installs a jGNEFilter which can properly call a callback from within
  30.     |        the application. The callback handles incoming events wither either 
  31.     |        68k or PPC code, and gets passed the data pointer you send from here.
  32.     |        Install() returns a pointer the jGNEFilter installed in the system heap
  33.     |        or NIL to indicate an error.
  34.  
  35.     /    Ptr ReleaseEventFilter(Ptr filterProc);
  36.     |        Pass in the pointer to the jGNEFilter, and this routine will disable 
  37.     |        the handling and try to dispose of its storage in the System Heap.
  38.     |        You *must* do this if your helper function is going to disappear when
  39.     |        the application closes. 
  40.     
  41.     /    asm void EventFilter(void);
  42.     |        This is *not* the installed jGNEFilter... well not really. There is no way
  43.     |        to get the same code to compile on both 68K and PPC, so this routine is
  44.     |        just a demo function. The real code has been disassembled from the 68k
  45.     |        source and placed into a Hex String suitable for StuffHex()'ing into the
  46.     |        System Heap on both 68K and PPC machines. Note that the code simply calls 
  47.     |        the ProcPtr for the Helper function blindly... whether 68k or PPC. 
  48.     |        WARNING: Changing the EventFilter() will do nothing! -- to apply changes, 
  49.     |        you need to take the modified 68K machine code and refresh the declared
  50.     |        kGNEFilterHexData string constant.
  51.  
  52.     /    void EventFilterHelper(EventRecord *theEvent, Ptr helperData);
  53.     |        This callback is the workhorse of the event filter. Once installed, this  
  54.     |        routine sees every event that gets harvested and has an opportunity to
  55.     |        modify the record before the front application gets to see it. Ideally
  56.     |        this function can do the necessary work itself or pass off the event info
  57.     |        to the home application.
  58.     |        WARNING: For 68k code, the routine will be called from the current app's
  59.     |        context (A5/Globals, Rsrc File, HeapZone). PPC code will have a valid RTOC
  60.     |        (Globals access) but not Rsrc File, HeapZone, etc.
  61.     
  62.  
  63.     How this all works:
  64.         
  65.         Since I didn't want to write the jGNEFilter in C (OK, I couldn't figure out
  66.         how to), the code is installed as raw 68k by StuffHex()'ing a precompiled
  67.         routine -- identical to the one as declared below -- into the System Heap.
  68.         
  69.         The jGNEFilter keeps 3 pieces of data inline: the next filter in the chain,
  70.         a pointer to the helper routine, and some extra data to pass to the helper.
  71.         Most importantly, we may not be able to remove the filter from the calling
  72.         chain...  the architecture just doesn't permit it! If we are able to safely
  73.         pull the filter out, we do. Otherwise the next best solution is to keep a flag,
  74.         that we can clear when we want to disable the functionality -- in fact, we
  75.         set or clear the pointer to the Helper Proc as the flag.
  76.         
  77.         Finally, the helper function is the meat of our jGNEFilter; it does the work
  78.         of the active filter. In the case of a 68K helper, it is accessed via a 
  79.         simple ProcPtr. In the case of a PPC helper, the installer sets up a valid
  80.         RoutineDescriptor (in the System Heap with the jGNEFilter) to invoke a 
  81.         MixedMode switch between the 68K caller (filter) and PPC routine (helper).
  82.         Again, when releasing the filter the handler disposes the descriptor and 
  83.         clear the inline ProcPtr/flag, since the helper function will probably be
  84.         disappearing when the application quits.
  85.         
  86.         If you are picking out events to handle within your app, my suggestion is to
  87.         keep a secondary Queue of events in the System Heap -- remember, you must 
  88.         allocate new EventRecords (since the current event belongs to the calling
  89.         app) into the System Heap (you need a heap that both the current process and
  90.         your own process can access). Given these events, your main event loop can 
  91.         suck out clicks or keydowns for dispatching internally and safely within your
  92.         own context.
  93.         
  94.         Also, Text Service windows don't receive Activate or Update events... you 
  95.         must check for those manually within your own event loop and handle them.
  96.     
  97. */
  98.  
  99. // ***********************************************************************************
  100. // ***********************************************************************************
  101.  
  102. Ptr InstallEventFilter(FilterHelperProcPtr helperProc, Ptr helperData) {
  103.     Ptr filterProc, data;
  104.     
  105.     // Create a duplicate function in the System Heap (so its *alway* there) and
  106.     //   copy the data across. Note: even though it is technically a function, we
  107.     //   can still treat it as data safely until it has been installed and called.
  108.     filterProc = NewPtrSys(sizeof(kGNEFilterHexData)/2 + 1);
  109.     if (! filterProc) return(0);
  110.     StuffHex(filterProc, kGNEFilterHexData);
  111.     
  112.     // Get and install the current filter as the next filter in the chain.
  113.     data = (Ptr) GetJGNEFilter();
  114.     BlockMove(&data, filterProc + kNextFilterOffset, sizeof(data));
  115.  
  116.     // Get and install the Helper function to do the real work (and as a flag to
  117.     //   indicate we are in business and accepting events). Remember that if we
  118.     //   generating PPC code, it is necessary to establish a Routine Descriptor.
  119.     SetZone(SystemZone());
  120.     data = (Ptr) helperProc;
  121.     if (! data) return(0);
  122.     BlockMove(&data, filterProc + kEventHelperOffset, sizeof(data));
  123.     SetZone(ApplicZone());
  124.  
  125.     // If the caller wants to pass data to the jGNEFilter Helper function. This
  126.     //   pointer (or handle if desired) *must* be allocated in the System Heap
  127.     //   if you don't plan on releasing the Filter before quitting. If you plan
  128.     //   on releasing the filter, then either the App or Sys heap will suffice.
  129.     data = helperData;
  130.     BlockMove(&helperData, filterProc + kEventHelperDataOffset, sizeof(data));
  131.     
  132.     // Install us, we are ready to do some work!
  133.     SetJGNEFilter((ProcPtr) filterProc);
  134.     
  135.     return(filterProc);
  136.     }
  137.  
  138. // ***********************************************************************************
  139. // ***********************************************************************************
  140.  
  141. Ptr ReleaseEventFilter(Ptr filterProc) {
  142.     Ptr data;
  143.     
  144.     if (! filterProc) return(0);
  145.  
  146.     // Clear the Helper location as an indicator that we have closed up shop. The
  147.     //   filter itself may lingers in the System Heap until shutdown unless we can
  148.     //   find a way to extract it from the chain (see below). On the other hand, the 
  149.     //   filter has been written so that if the Helper function pointer is NIL, the 
  150.     //   filter will do nothing at all. Let's zero it out for that (hopeful) case.
  151.     data = 0;
  152.     BlockMove(&data, filterProc + kEventHelperOffset, sizeof(data));
  153.     
  154.  
  155.     // If the installed filterProc is the first one in the chain, then we should
  156.     //    be able remove it and replace it with the next one (the one we would
  157.     //    normally jump to). If we can dispose the filterProc buffer, then we can
  158.     //    can recover those 50 bytes that remain in the System Heap.
  159.     // Thanks to HoverBar's Guy Fullerton (hedgeboy@realm.net) for the suggestion.
  160.     if (filterProc == (Ptr) GetJGNEFilter()) {
  161.         // Remove our filterProc from the chain.
  162.         BlockMove(filterProc + kNextFilterOffset, &data, sizeof(data));
  163.         SetJGNEFilter((ProcPtr) data);
  164.         
  165.         BlockMove(filterProc + kEventHelperDataOffset, &data, sizeof(data));
  166.         DisposePtr(filterProc);
  167.         }
  168.       else {
  169.         // Grab the data that was passed when initialized or as set in the Helper
  170.         //   function. The caller can then deallocate it if desired or necessary.
  171.         
  172.         BlockMove(filterProc + kEventHelperDataOffset, &data, sizeof(data));
  173.         }
  174.     
  175.     return(data);
  176.     }
  177.  
  178. // ***********************************************************************************
  179. // ***********************************************************************************
  180.  
  181. #if GENERATING68K
  182. asm void EventFilter() {
  183.     bra.s    Continue
  184.         
  185. Next_Filter:
  186.     dc.l    0                        // Saved Address of Next jGNEFilter in the chain.
  187.                                     //   We jump directly to it, no JSR's or RTS's.
  188. Event_Helper:
  189.     dc.l    0                        // Pointer to Helper function in our application.
  190.                                     //   We clear it to NIL when we quit as a flag
  191. Event_Helper_Data:
  192.     dc.l    0                        // Promised storage for the Helper function, 
  193.                                     //   which can modify the pointer dynamically.
  194. Continue:
  195.  
  196. // FRALLOC is a MW macro that simplifies ASM local vars and
  197. //   stack maintenance -- use it if we got it, its very easy
  198. #ifdef ____USE_FRALLOC____
  199.     fralloc
  200. #else
  201.     link    a6, #0
  202. #endif ____USE_FRALLOC____
  203.  
  204.     // Save the Volatile registers for safety
  205.     movem.l    d0-d2/a0-a2, -(a7)            
  206.  
  207.     // Load the Helper from Inline Storage and test it. If Helper is NIL,
  208.     //   then our handler was released -- and we just jump to the next Filter
  209.     move.l    Event_Helper, a0
  210.     move.l    a0, d0
  211.     tst.l    d0                            
  212.     beq        End_Filter
  213.     
  214.     // Straight C Calling Conventions, call the Helper function
  215.     move.l    Event_Helper_Data, -(a7)
  216.     move.l    a1, -(a7)
  217.     move.l    Event_Helper, a0
  218.     jsr        (a0)
  219.     add.l    #8, a7
  220.         
  221. End_Filter:
  222.  
  223.     // Clean up the same way we got here
  224.     movem.l    (a7)+, d0-d2/a0-a2
  225.  
  226. #ifdef ____USE_FRALLOC____
  227.     frfree
  228. #else
  229.     unlk    a6
  230. #endif ____USE_FRALLOC____
  231.     
  232.     // Jump to the next filter in the chain
  233.     move.l    Next_Filter, a0
  234.     jmp        (a0)
  235.     }
  236. void EndEventFilter() { }
  237. #endif GENERATING68K
  238.  
  239. // ***********************************************************************************
  240. // ***********************************************************************************
  241.  
  242. void EventFilterHelper(EventRecord *theEvent, Ptr helperData) {
  243.     Boolean fwdThisEvent = FALSE, filterThisEvent = FALSE;
  244.     long saveA5;
  245.     EvQEl *fwdEvent;
  246.     
  247.     // This only does something in 68K code. We now have access to globals,
  248.     //   which PPC get for free from CFM; however, we won't have access to 
  249.     //   our application's Resource file/chain or HeapZone. Be careful!
  250.     saveA5 = SetA5((long) helperData);
  251.     
  252.     // Check to see if the floaters should all be hidden, then hide or show any
  253.     //   window's as necessary. Note: Update events will be entered/handled in
  254.     //   our context, so let our app get the CPU to handle the event quickly
  255.     TestScreenSaver();
  256.     if (ShowHideFloater(0)) WakeUpProcess(&glob.myPSN);
  257.     
  258.     switch(theEvent->what) {
  259.         case nullEvent:
  260.             break;
  261.         case mouseDown: {
  262.             short thePart, index;
  263.             Boolean found;
  264.             WindowPtr whichWin;
  265.             FloaterQElemPtr floatQElem;
  266.             
  267.             // The following tests shouldnt be necessary... but sometimes there are
  268.             //   problems with the FindServiceWindow() call. To get around that,
  269.             //   eliminate a few checks in some cases that we know are wrong.
  270.         
  271.             for(found = FALSE, floatQElem = GetIndFloater(index = 1, FALSE);
  272.                     floatQElem && !found; floatQElem = GetIndFloater(++index, FALSE)) {
  273.                 if (PtInRgn(theEvent->where,
  274.                         ((WindowPeek) floatQElem->floatWindow)->strucRgn)) found = TRUE;
  275.                 }
  276.             if (! found) break;
  277.             
  278.             thePart = FindServiceWindow(theEvent->where, &whichWin);
  279.             theEvent->message = thePart;
  280.             switch(thePart) {
  281.                 case inMenuBar:
  282.                 case inSysWindow:
  283.                     break;
  284.                 case inDrag:
  285.                 case inContent:
  286.                 case inGrow:
  287.                 case inGoAway:
  288.                 case inZoomIn:
  289.                 case inZoomOut:
  290.                     fwdThisEvent = TRUE;
  291.                     filterThisEvent = TRUE;
  292.                     break;
  293.                 }
  294.             }
  295.             break;
  296.         case keyDown:
  297.         case autoKey: {
  298.             char theKey, theChar;
  299.             
  300.             theChar = theEvent->message & charCodeMask;
  301.             theKey = (theEvent->message & keyCodeMask) >> 8;
  302.             
  303.             // Add your selected tests for key-shortcuts
  304.             if ((theEvent->modifiers & cmdKey) && (theKey == 0x35)) {
  305.                 // Cmd-Escape is the sequence to temporarily hide floaters
  306.                 //   and a wakeup to handle any pending update events
  307.                 glob.hideFloats = (glob.hideFloats) ? FALSE : TRUE;
  308.                 if (ShowHideFloater(0)) WakeUpProcess(&glob.myPSN);
  309.  
  310.                 fwdThisEvent = filterThisEvent = FALSE;
  311.                 }
  312.             }
  313.             break;
  314.         default:
  315.             break;
  316.         }
  317.     
  318.  
  319.     if (fwdThisEvent) {
  320.         // We have discovered that this event deserves full attention. Forward it to
  321.         //   our home application by Q'ing it up as a pointer in the System Heap
  322.         fwdEvent = (EvQEl *) NewPtrSys(sizeof(*fwdEvent));
  323.         fwdEvent->qLink = 0;
  324.         fwdEvent->qType = evType;
  325.         BlockMove(&theEvent->what, &fwdEvent->evtQWhat, sizeof(EventRecord));
  326.         Enqueue((QElem *) fwdEvent, &glob.forwardedEvents);
  327.         
  328.         // Let our app get the CPU to handle the event quickly
  329.         WakeUpProcess(&glob.myPSN);
  330.         }
  331.         
  332.     // Does intercepting the event mean no one else should have it?
  333.     if (filterThisEvent) theEvent->what = nullEvent;
  334.  
  335.     // Restore the (68K) context before leaving
  336.     SetA5(saveA5);
  337.     }
  338.  
  339.